<template>
  <div id="app" v-bind:style="appDivStyle">
      <div class="context" ref="context">
           <div class="queryBox" ref="queryBox">
                <el-form :model="formQueryCondition"
                         ref="form" size="mini">
                    <el-row>
                        <!-- 版权提供方名称 -->
                        <el-col :span="5">
                            <el-form-item :label="$t('PublisherManage.queryLabel.providerName')"   :label-width="queryformLabelWidthLong">
                                <el-input v-model.trim="formQueryCondition.publisherName.val" class="queryItemStyle" clearable
                                        :placeholder="$t('PublisherManage.queryLabel.placeholderProviderName')">
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <!-- 状态 -->
                        <el-col :span="5" :offset="1">
                            <el-form-item :label="$t('PublisherManage.queryLabel.status')"  :label-width="queryformLabelWidth">
                                <el-select v-model="formQueryCondition.isEnable.val" class="queryItemStyle">
                                    <el-option :label="$t('label.all')" value=""></el-option>
                                    <el-option :value="drug.valueCode" :key="drug.valueCode" :label="drug.valueText" v-for="drug in enables"></el-option>
                                    <!-- <el-option :label="$t('label.available')" value="Available"></el-option>
                                    <el-option :label="$t('label.invalid')" value="Invalid"></el-option> -->
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 列表上的按钮 -->
                    <el-row>
                        <el-col :span="24">
                            <div style="text-align:right">
                                <el-form-item>
                                    <el-button v-permission="{menuCode: menuCode, resourceCode: 'saveBtnText'}"
                                    type="primary" v-if="test('onNewBtn')"
                                    @click="onNew('saveBtnText')">{{ $t('btnText.newBtnText') }}</el-button>
                                <!-- </el-form-item> -->
                                <!-- <el-form-item> -->
                                    <el-button v-permission="{menuCode: menuCode, resourceCode: 'resetBtnText'}"
                                    type="primary" @click="onReset('form')">{{ $t('btnText.resetBtnText') }}</el-button>
                                <!-- </el-form-item> -->
                                <!-- <el-form-item>
                                    <el-button type="primary" @click="onExport" icon="el-icon-download" :loading="loading">{{ $t('btnText.exportBtnText') }}</el-button>
                                </el-form-item> -->
                                <!-- <el-form-item> -->
                                    <el-button v-permission="{menuCode: menuCode, resourceCode: 'queryBtnText'}"
                                    type="primary" @click="onQuery('queryBtnText')" icon="el-icon-search" :loading="loading">{{ $t('btnText.queryBtnText') }}</el-button>
                                </el-form-item>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div class="dataBox" ref="dataBox">

                <transition enter-active-class="animated zoomIn" leave-active-class="animated rotateOut">
                    <el-table v-if="isInit" ref="singleTable"
                                :data="tableData"
                                :height="tableHeight"
                                @sort-change="sortChange"
                                v-loading="loading" size="mini"
                                style="width: 100%;" stripe=stripe border=border>
                        <el-table-column type="index"  width="50" fixed="left"
                                            :label="$t('table.xh')" align="center">
                        </el-table-column>
                        <el-table-column :label="$t('table.op')" fixed="left" min-width="230">
                            <template slot-scope="scope">
                                <el-button size="gv" icon="el-icon-edit" :loading="loading"
                                v-permission="{menuCode: menuCode, resourceCode: 'editBtnText'}"
                                @click="edit(scope.$index, scope.row,'editBtnText')"
                                            >{{ $t('btnText.editBtnText') }}</el-button>
                                <!--下发-->
                                <el-button size="gv" icon="el-icon-delete"
                                    v-permission="{menuCode: menuCode, resourceCode: 'sendBtnText'}"
                                        type="danger" :loading="loading" @click="onSend(scope.$index, scope.row,'sendBtnText')"
                                       :disabled="scope.row.sendtoState!='StayDown'" >{{ $t('ParentAssetIssuance.btnText.sendBtnText') }}</el-button>
                                <!-- 注销 -->
                                <el-button size="gv" icon="el-icon-delete" v-show="scope.row.isEnable=='Available'" v-permission="{menuCode: menuCode,resourceCode: 'passBtnText'}"
                                      type="danger" :loading="loading" @click="logoffRow(scope.$index, scope.row,'passBtnText')"
                                     >{{ $t('btnText.logoffBtnText') }}</el-button>
                                <!-- 恢复 -->
                                <el-button size="gv" icon="el-icon-delete" v-show="scope.row.isEnable==='Invalid'"
                                v-permission="{menuCode: menuCode,resourceCode: 'recoveryBtnText'}"
                                      type="danger" :loading="loading" @click="revertLogoffRow(scope.$index, scope.row,'recoveryBtnText')"
                                     >{{ $t('btnText.recoveryBtnText') }}</el-button>
                                <!-- 删除 -->
                                <el-button size="gv" icon="el-icon-delete" v-show="scope.row.isEnable==='Invalid' && scope.row.sendtoState!='StayDown'"
                                v-permission="{menuCode: menuCode,resourceCode: 'delBtnText'}"
                                      type="danger" :loading="loading" @click="deleteRow(scope.$index, scope.row,'delBtnText')"
                                     >{{ $t('btnText.deleteBtnText') }}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column prop="keyId" min-width="140" fixed="left" v-if="false">
                        </el-table-column>
                        <el-table-column prop="publisherCode" min-width="140" fixed="left"
                                            :label="$t('PublisherManage.table.providerCode')" sortable="custom" sort-by="publisherCode">
                        </el-table-column>
                        <el-table-column prop="publisherName" min-width="140" fixed="left"
                                            :label="$t('PublisherManage.table.providerName')" sortable="custom" sort-by="publisherName">
                        </el-table-column>
                        <el-table-column prop="contact" min-width="140"
                        :label="$t('PublisherManage.table.providerTelephone')" sortable="custom" sort-by="contact">
                        </el-table-column>
                        <el-table-column prop="comAddress" min-width="150"
                        :label="$t('PublisherManage.table.address')" sortable="custom" sort-by="comAddress">
                        </el-table-column>
                        <!--下发状态-->
                    <el-table-column prop="sendtoStateName" min-width="110"
                                        :label="$t('ParentAssetIssuance.table.ParentAssetIssuance.status')" sortable="custom" sort-by="sendtoState"></el-table-column>
                        <el-table-column prop="isEnableName" min-width="110" sortable="custom"
                        :label="$t('PublisherManage.table.status')" sort-by="isEnable">
                        </el-table-column>

                        <el-table-column :label="$t('PublisherManage.table.lastUpdatedBy')" min-width="120"
                        sortable="custom" prop="modifier" sort-by="modifier">
                        </el-table-column>
                        <el-table-column :label="$t('PublisherManage.table.lastUpdateTime')" min-width="180"
                        sortable="custom" prop="lastUpdatedDate" sort-by="lastUpdatedDate">
                        </el-table-column>

                    </el-table>
                </transition>

            </div>
        </div>
        <div class="pageBox">
            <el-pagination @size-change="sizeChange"
                            @current-change="currentPageChange"
                            :current-page="pager.curPage"
                            :page-sizes="pager.sizes"
                            :page-size="pager.curSize"
                            :background="true"
                            layout="total, sizes, prev, pager, next, jumper" :total="pager.total">
            </el-pagination>
        </div>

         <!--编辑对话框-->
        <transition enter-active-class="animated rotateIn" leave-active-class="animated rotateOut">
            <el-dialog :title="$t('label.dialogTitle')" :visible.sync="isShowEditForm" top="5vh" :close-on-click-modal="false">
                <el-form :model="editForm" ref="editForm" size="mini"
                        :label-position="editFormPosition" status-icon=status-icon :rules="editRules">
                    <el-form-item
                        :label-width="editformLabelWidth" prop="keyId" v-if="false">
                        <el-input v-model.trim="editForm.keyId" maxlength="20" ></el-input>
                    </el-form-item>
                    <el-form-item :label="$t('PublisherManage.editLabel.providerCode')"
                        :label-width="editformLabelWidth" prop="publisherCode">
                        <el-input v-model.trim="editForm.publisherCode" maxlength="20" :disabled="canEditCode"
                                :placeholder="$t('PublisherManage.editLabel.placeholderProviderCode')"></el-input>
                    </el-form-item>
                    <el-form-item :label="$t('PublisherManage.editLabel.providerName')"
                        :label-width="editformLabelWidth" prop="publisherName">
                        <el-input v-model.trim="editForm.publisherName" maxlength="50"
                                :placeholder="$t('PublisherManage.rules.placeholderProviderName')"></el-input>
                    </el-form-item>

                    <el-form-item :label="$t('PublisherManage.editLabel.providerTelephone')"
                        :label-width="editformLabelWidth" prop="contact">
                        <el-input v-model.trim="editForm.contact" maxlength="50"
                                :placeholder="$t('PublisherManage.editLabel.placeholderProviderTelephone')"></el-input>
                    </el-form-item>

                    <el-form-item :label="$t('PublisherManage.editLabel.address')"
                        :label-width="editformLabelWidth" prop="comAddress">
                        <el-input v-model="editForm.comAddress" maxlength="100" type="textarea"
                                :placeholder="$t('PublisherManage.editLabel.placeholderAddress')"></el-input>
                    </el-form-item>
                    <el-form-item :label="$t('PublisherManage.editLabel.status')"
                        :label-width="editformLabelWidth" prop="isEnable">
                        <el-select v-model="editForm.isEnable" class="queryItemStyle">
                            <!-- <el-option :label="$t('label.available')" value="Available"></el-option>
                            <el-option :label="$t('label.invalid')" value="Invalid"></el-option> -->
                            <el-option :value="drug.valueCode" :key="drug.valueCode" :label="drug.valueText" v-for="drug in enables"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer">
                    <el-button size="mini" @click="isShowEditForm = false" :loading="loading">{{ $t('btnText.cancelBtnText') }}</el-button>
                    <el-button size="mini" type="primary" @click="submitForm('editForm',currentResCode)" :loading="loading">{{ $t('btnText.saveBtnText') }}</el-button>
                </div>
            </el-dialog>
        </transition>

  </div>


</template>

<script>

import comBaseVue from '../../baseViewModel';

export default {
  mixins: [comBaseVue],
  name:'PublisherManage',
  props:{
      menuCode: String,
  },
  data () {

    return {
        queryformLabelWidthLong:"110px",
        isInit:false,
        tableHeight:100,
        enables: [],
        singleSend:{},
        appDivStyle:{
            height:this.$store.getters.appHeight,
        },
        modelName:"com.ispr.iptv.entity.sys.SysPublisher",
        // 查询条件
        formQueryCondition: {
            publisherName: {
                key:'publisherName',
                op: 'LIKE', // 操作符
                val: '' // 查询的值
            },
            isEnable: {
                key:'isEnable',
                op:'=',
                val: ''
            },
        },

        // 编辑面板绑定的对象
        editForm: {
            keyId: 0,
            version: 0,
            publisherCode: "",
            publisherName: "",
            contact: "",
            comAddress: "",
            sendtoState: "StayDown",
            isEnable: "",
        },
        // 编辑面板的验证规则
        editRules: {
            publisherCode: [{ required: true, message: this.$i18n.t('PublisherManage.rules.placeholderProviderCode'), trigger: 'blur' }],
            publisherName: [{ required: true, message: this.$i18n.t('PublisherManage.rules.placeholderProviderName'), trigger: 'blur' }],
            // providerTelephone: [{ required: true, message: this.$i18n.t('PublisherManage.rules.placeholderProviderTelephone'), trigger: 'blur' }],
            // address: [{ required: true, message: this.$i18n.t('PublisherManage.rules.placeholderAddress'), trigger: 'blur' }]
        },
        sendType:"",
    }
  },
  methods:{
      test:function(btnCode){
          return true;
        },
    // 新增
    onNew: function(rescode) {
        this.currentResCode = (rescode && rescode!=='') ? rescode : '';
        this.isShowEditForm = true;
        if (this.$refs['editForm']) {
            this.$refs['editForm'].resetFields();
        }
        this.editForm.keyId = "";
        this.editForm.version = "";
        this.editForm.publisherCode = "";
        this.editForm.publisherName = "";
        this.editForm.contact = "";
        this.editForm.comAddress = "";
        this.editForm.sendtoState = "StayDown";
        this.editForm.isEnable = "Available";
        // this.editForm.isEnabled = "Available";
        // this.curCommand = "SaveNew";
        this.curCommand = "CopyrightProviderProc";
        this.canEditCode = false;
    },

    setEditFormCheck: function(){
        if(this.curCommand == "CopyrightProviderProc"){
            this.editFormCheck = [{
                key: "publisherCode",
                op: "=",
                val: this.editForm.publisherCode
            },
            // {
            //     key: "publisherName",
            //     op: "=",
            //     val: this.editForm.publisherName
            // }
            ];
        }else if(this.curCommand == "CopyrightProviderProcEdit"){
            this.editFormCheck = [{
                key: "publisherCode",
                // group: "or",
                op: "=",
                val: this.editForm.publisherCode
            },
            // ,{
            //     key: "publisherName",
            //     group: "or",
            //     op: "=",
            //     val: this.editForm.publisherName
            // }
            {
                key: "keyId",
                op: "<>",
                val: this.editForm.keyId
            }];
        }
    },
    //单个下发
    onSend: function(index, row, rescode) {
        var _this = this;
        this.currentResCode = (rescode && rescode!=='') ? rescode : '';
        this.sendType = "single";
        this.singleSend = {};
        let data = {
          "keyId":row.keyId,
          "version":row.version,
          "resType":"PUBLISHER",
        };
        let dataList = [];//最终传给后台的总数据
        dataList.push(data);
        this.$confirm(this.$i18n.t("message.confirmSendMessage"),
            this.$i18n.t("message.confirmTitle"), {
            confirmButtonText: this.$i18n.t("message.yes"),
            cancelButtonText: this.$i18n.t("message.cancel"),
            type: "warning",
            callback: function (action, instance) {
                if (action == "confirm") {
                    _this.loading = true;
                    _this.procCommand("MediaSendToProc",
                    _this.modelName,
                    "Default",
                    {
                      "dataList":dataList,
                    },
                    null,
                    _this.submitFormCallback);
                }
            }
        });
    },

     // 重置
    onReset:function(){
        if (this.$refs['form']) {
            this.$refs['form'].resetFields();
        }
        this.formQueryCondition.publisherName.val = "";
        this.formQueryCondition.isEnable.val = "";
        this.formQueryCondition.isEnable.val = "Available";
    },

    // 注销按钮
        logoffRow: function(index, row, rescode) {
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            var _this = this;
            this.$confirm(this.$i18n.t("message.confirmlogoffMessage"),
                this.$i18n.t("message.confirmTitle"), {
                confirmButtonText: this.$i18n.t("message.yes"),
                cancelButtonText: this.$i18n.t("message.cancel"),
                type: "warning",
                callback: function (action, instance) {
                    if (action == "confirm") {
                        var commandBody = {  "keyId": row.keyId, "version": row.version, isEnable: "Invalid", sendtoState:"StayDown" };
                        _this.loading = true;
                        _this.procCommand("PassProc",
                                _this.modelName,
                                "N",
                                commandBody,
                                null,
                                _this.submitFormCallback);
                    }

                }
            });
        },
        // 恢复注销按钮
        revertLogoffRow: function(index, row, rescode) {
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            var _this = this;
            this.$confirm(this.$i18n.t("message.confirmNormalMessage"),
                this.$i18n.t("message.confirmTitle"), {
                confirmButtonText: this.$i18n.t("message.yes"),
                cancelButtonText: this.$i18n.t("message.cancel"),
                type: "warning",
                callback: function (action, instance) {
                    if (action == "confirm") {
                        var commandBody = {  "keyId": row.keyId, "version": row.version, isEnable: "Available", sendtoState:"StayDown" };
                        _this.loading = true;
                        _this.procCommand("RecoveryProc",
                                _this.modelName,
                                "N",
                                commandBody,
                                null,
                                _this.submitFormCallback);
                    }

                }
            });
        },


    // 授权按钮单击事件
    editMenu: function(index, row) {

    },

    edit: function(index, row,rescode) {
        this.currentResCode = (rescode && rescode!=='') ? rescode : '';
        // this.queryModelByKeyID(this.modelName,row.keyId,"QueryForEdit",this.editCallback);
        this.queryModelByKeyID(this.modelName,row.keyId,"ById",this.editCallback);
    },
    // 编辑按钮回调事件
    editCallback: function(isSuccess,json) {
        if(isSuccess){
            this.curCommand = "CopyrightProviderProcEdit";
            this.isShowEditForm = true;
            if (this.$refs['editForm']) {
                this.$refs['editForm'].resetFields();
            }
            // this.editForm = json;

            this.editForm.keyId = json.keyId;
            this.editForm.version = json.version;
            this.editForm.publisherCode = json.publisherCode;
            this.editForm.publisherName = json.publisherName;
            this.editForm.contact = json.contact;
            this.editForm.comAddress = json.comAddress;
            this.editForm.sendtoState = "StayDown";
            this.editForm.isEnable = json.isEnable;

            this.canEditCode = true;
        }
        else{
            this.$message({
                message: json.msg,
                type: 'warning'
                });
        }
    },
    initTable:function(){
        this.tableHeight = this.$refs.context.offsetHeight - this.$refs.queryBox.offsetHeight;
        this.isInit = true;
    },
     // 查询按钮
        onQuery: function(rescode) {
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            this.pager.curPage = 1;
            this.queryTable();
        },
        // 查询网格数据
        queryTable: function() {
            this.loading = true;
            this.queryModel(this.modelName,"Query",this.formQueryCondition,this.pager,this.sort,this.setTableData)
        },
        // 查询网格数据成功后的回调
        setTableData: function(isSuccess,json) {
            this.currentResCode = '';
            this.$nextTick(function() {
                this.loading = false;
            });
            try{
                if (isSuccess) {
                    //alert(JSON.stringify(json));
                    this.pager.total = json.total;
                    this.tableData = [];
                    for (var i = 0; i < json.list.length; i++) {
                        this.tableData.push(json.list[i]);
                    }

                }
                else {
                    this.$message.error(json.msg);
                }
            }
            catch(e){
                alert(e);
            }
        },
    // 为状态下拉框赋值
    findEnables: function() {
        this.findByTypeCode("10001",this.setEnables)
    },
    // 查询状态下拉框数据成功后的回调
    setEnables: function(isSuccess,json) {
        try{
            if (isSuccess) {
                // this.enables = [];
                for (var i = 0; i < json.data.length; i++) {
                    this.enables.push(json.data[i]);
                }
            }
            else {
                this.$message.error(json.msg);
            }
        }
        catch(e){
            alert(e);
        }
    },
      enterDown(){
          var g = this
          if(g.isShowEditForm){
              g.submitForm('editForm',g.currentResCode)
          }else if(!g.loading){
              g.onQuery()
          }
      }


  },
  mounted: function mounted() {
    this.findEnables();
    // this.tableHeight = this.$refs.context.offsetHeight - this.$refs.queryBox.offsetHeight;
    this.formQueryCondition.isEnable.val = "Available";
    this.isInit = true;
    this.onQuery();
    window.setTimeout(this.initTable,100);

  }
}


</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
